package br.com.sgpv2.hibernate.nivel;

import java.util.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import br.com.sgpv2.entity.organograma.Nivel;
import br.com.sgpv2.entity.pessoa.Contrato;
import br.com.sgpv2.entity.pessoa.Pessoa;
import br.com.sgpv2.entity.pessoa.PessoaDTO;
import br.com.sgpv2.hibernate.infra.SGPGenericDAO;

@Repository(value="nivelRepository")
public class NivelHibernateDAO extends SGPGenericDAO<Nivel> implements NivelRepository{
	
	private static final String NULL = "null";
	
	/**
	 * Construtor padrão.
	 * 
	 */
	@Autowired
	public NivelHibernateDAO(
			@Qualifier("sessionFactory") SessionFactory factory) {
		super(Nivel.class);
		super.setSessionFactory(factory);
	}

	@Override
	public List<Contrato> listarContratosPorDepartamento(int idCargo) {
		Criteria criteria = createCriteria(Contrato.class);
		criteria.createAlias("nivel", "n");
		criteria.add(Restrictions.eq("n.id", idCargo));

		return Collections.checkedList((List<Contrato>) criteria.list(),
				Contrato.class);
	}

	@Override
	public Nivel buscarNivelPorCodigo(String nivel) {
		Criteria criteria = createCriteria(Nivel.class);
		criteria.add(Restrictions.eq("codigo", nivel));
		return (Nivel) criteria.uniqueResult();
	}
	
	public List<Nivel> listarOrder(String atributoOrder) {
		Criteria criteria = createCriteria(Nivel.class);
		criteria.addOrder(Order.asc(atributoOrder));
		return Collections.checkedList((List<Nivel>) criteria.list(),
				Nivel.class);
	}

	@Override
	public List<Nivel> consultarNivelPorDescricao(String descricao) {
		Criteria criteria = createCriteria(Nivel.class);

		if (descricao != null && !descricao.isEmpty() && !descricao.equals(NULL)) {
			criteria.add(Restrictions.like("descricao", descricao, MatchMode.ANYWHERE));
		}

		return Collections.checkedList((List<Nivel>) criteria.list(),
				Nivel.class);
	}

}
